/*
 * Copyright (C), 2014-2015, 联创车盟汽车服务有限公司
 * FileName: UserManageController.java
 * Author:   ryan
 * Date:     2015年2月2日 上午9:44:33
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>    <time>    <version>    <desc>
 * 修改人姓名        修改时间        版本号        描述
 */
package com.linkage.controller.background;

import java.util.List;
import java.util.Map;




import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.interceptor.NeedAdminLogin;
import com.linkage.pojo.commodity.Book;
import com.linkage.pojo.commodity.Category;
import com.linkage.pojo.commodity.SpecialBook;
import com.linkage.service.background.BookManageService;
import com.linkage.service.background.UserManageService;
import com.linkage.service.commodity.CategoryService;
import com.linkage.service.commodity.SpecialBookService;

/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author ryan
 *
 */

@Controller
@RequestMapping("/admin/book")
public class BookManageController {

    @Autowired
    private UserManageService userManageService;
    
    @Autowired
    private BookManageService bookManageService;
    
    @Autowired
    private SpecialBookService specialBookService;
    
    @Autowired
    private CategoryService categoryService;

    @NeedAdminLogin
    @RequestMapping(value = "/booklist", method = RequestMethod.GET)
    public String list(Model model) {
        List<Map<String, Object>> books = this.bookManageService.listAllBooks();
        model.addAttribute("books",books );

        return "background/book/booklist";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/booklist/{bookId}/{newStatus}", method = RequestMethod.GET)
    public String delete(@PathVariable("bookId") Integer bookId,@PathVariable("newStatus") Boolean newStatus) {
      
        this.bookManageService.changeBookStatus(bookId, newStatus);
        return "redirect:/admin/book/booklist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/specialslist", method = RequestMethod.GET)
    public String specialslist(Model model) {
        
        List<Map<String, Object>> specials= this.bookManageService.listSpecialList();

        model.addAttribute("specials",specials);

        return "background/book/specialslist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/specialslist/delete/{special_id}", method = RequestMethod.GET)
    public String delete( @PathVariable("special_id") Integer special_id) {

        this.specialBookService.delete(special_id);
        return "redirect:/admin/book/specialslist";
    }
    
    
    @NeedAdminLogin
    @RequestMapping(value="/booklist/addspecial",method = RequestMethod.GET)
    public String addSpecial(@RequestParam("bookId")Integer bookId,@RequestParam("specialPrice")Float specialPrice,HttpServletRequest request,Model model){
        this.specialBookService.createSpecialBook(request, bookId, specialPrice);
        return "redirect:/admin/book/booklist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/addbook", method = RequestMethod.GET)
    public String addView(Model model) {
        
        List<Category> categories = this.categoryService.listAllCategory();
        model.addAttribute("categories", categories);
        return "background/book/addbook";
    }
    
    
    @NeedAdminLogin
    @RequestMapping(value = "/savebook", method = RequestMethod.POST)
    public String save(Book book ) {
        this.bookManageService.addNewBook(book);
        return "redirect:/admin/book/booklist";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/updatebook/{bookId}", method = RequestMethod.GET)
    public String updateView(Model model, @PathVariable("bookId") Integer bookId) {
        
        Book book=this.bookManageService.getBookByID(bookId);
        model.addAttribute("book", book);
        List<Category> categories = this.categoryService.listAllCategory();
        model.addAttribute("categories", categories);

        return "background/book/updatebook";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/updatebook", method = RequestMethod.POST)
    public String update(Book book) {
        this.bookManageService.editBook(book);
        
        return "redirect:/admin/book/booklist";
    }
    
    
    @NeedAdminLogin
    @RequestMapping(value = "/categorylist", method = RequestMethod.GET)
    public String category(Model model) {
        List<Category> categories = this.categoryService.listAllCategory();
        model.addAttribute("categories", categories);
        return "background/book/categorylist";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/categorylist/addcategory", method = RequestMethod.GET)
    public String addCategory(Category category) {
        this.categoryService.createCategory(category);
  
        return "redirect:/admin/book/categorylist";
    }
    
    
    
    
    
    
    
    
}

